<?php

/**
 * 前台Action
 * Some rights reserved：abc3210.com
 * Contact email:admin@abc3210.com
 */
class BaseAction extends AppframeAction {

    public $TemplatePath, $Theme, $ThemeDefault;

    function _initialize() {
        parent::_initialize();
        $this->initUser();
        //初始化模型
        $this->initModel();
        $this->tmpinit();
    }
    
    /**
     * 模板配置初始化 
     */
    final public function tmpinit(){
        //模板路径
        $this->TemplatePath = TEMPLATE_PATH;
        //默认主题风格
        $this->ThemeDefault = "Default";
        //主题风格
        $this->Theme = empty($this->Cache["Config"]['theme']) ? $this->ThemeDefault : $this->Cache["Config"]['theme'];
        //如果有指定 GROUP_MODULE 则模块名直接是GROUP_MODULE，否则使用 GROUP_NAME，这样做的目的是防止其他模块需要生成
        $group   =  defined('GROUP_MODULE')?GROUP_MODULE.'/':GROUP_NAME.'/';
        C('TEMPLATE_NAME',$this->TemplatePath.$this->Theme."/".$group.(THEME_NAME?THEME_NAME.'/':''));
        //模板路径
        C('THEME_PATH','/Template/'.$this->Theme.'/');
        //模板Css路径
        C('THEME_CSS_PATH','/Template/'.$this->Theme.'/Public/Css/');
        //模板Js路径
        C('THEME_JS_PATH','/Template/'.$this->Theme.'/Public/Js/');
        //模板Imgaes路径
        C('THEME_IMAGES_PATH','/Template/'.$this->Theme.'/Public/Images/');
        //设置前台提示信息模板
        if (file_exists_case($this->TemplatePath.$this->Theme."/"."error.html")){
            C("TMPL_ACTION_ERROR",$this->TemplatePath.$this->Theme."/"."error.html");
        }
        if (file_exists_case($this->TemplatePath.$this->Theme."/"."success.html")){
            C("TMPL_ACTION_SUCCESS",$this->TemplatePath.$this->Theme."/"."success.html");
        }
    }
    
    public function display($templateFile = '', $charset = '', $contentType = '') {
        parent::display($this->parseTemplateFile($templateFile), $charset, $contentType);
    }

    /**
     * 模板路径
     * @param type $templateFile
     * @return boolean|string 
     */
    public function parseTemplateFile($templateFile = '') {
        
        if ('' == $templateFile) {
            // 如果模板文件名为空 按照默认规则定位
            $templateFile = C('TEMPLATE_NAME').MODULE_NAME.(defined('GROUP_NAME')?C('TMPL_FILE_DEPR'):'/').ACTION_NAME.C('TMPL_TEMPLATE_SUFFIX');
        } elseif (false === strpos($templateFile, C('TMPL_TEMPLATE_SUFFIX'))) {
            // 解析规则为 模板主题:模块:操作 不支持 跨项目和跨分组调用
            $path = explode(':', $templateFile);
            $action = array_pop($path);
            $module = !empty($path) ? array_pop($path) : MODULE_NAME;
            
            if (!empty($path)) {// 设置模板主题
                $path = dirname(C("TEMPLATE_NAME")) . '/' . array_pop($path) . '/';
            } else {
                $path = C("TEMPLATE_NAME");
            }
            $depr = defined('GROUP_NAME') ? C('TMPL_FILE_DEPR') : '/';
            
            $templateFile = $path . $module . $depr . $action . C('TMPL_TEMPLATE_SUFFIX');
        }
        if (!file_exists_case($templateFile)){
            echo str_replace(SITE_PATH,"",$templateFile)."模板不存在！";exit;
        }
        return $templateFile;
    }

    /**
     * 分页
     * @param type $Total_Size
     * @param type $Page_Size
     * @param type $Current_Page
     * @param type $listRows
     * @param type $PageParam
     * @param type $PageLink
     * @param type $Static
     * @return \Page 
     */
    protected function page($Total_Size = 1, $Page_Size = 0, $Current_Page = 1, $listRows = 6, $PageParam = '', $PageLink = '', $Static = FALSE) {
        import('@.ORG.Util.Page');
        if ($Page_Size == 0) {
            $Page_Size = C("PAGE_LISTROWS");
        }
        if (empty($PageParam)) {
            $PageParam = C("VAR_PAGE");
        }
        $Page = new Page($Total_Size, $Page_Size, $Current_Page, $listRows, $PageParam, $PageLink, $Static);
        $Page->SetPager('Admin', '共有{recordcount}条信息&nbsp;{pageindex}/{pagecount}&nbsp;{first}{prev}&nbsp;{liststart}{list}{listend}&nbsp;{next}{last}&nbsp;转到{jump}页', array("listlong" => "6", "first" => "首页", "last" => "尾页", "prev" => "上一页", "next" => "下一页", "list" => "*", "disabledclass" => ""));
        return $Page;
    }
}

?>
